import Vue from 'vue'
import App from './App.vue'
import router from './router'

import axios from "axios";
Vue.prototype.$axios = axios;
axios.defaults.baseURL = 'http://liangwei.tech:3000'

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);

axios.interceptors.request.use(config => {
  if (!config.headers.Authorization && localStorage.getItem('token')) {
    config.headers.Authorization = localStorage.getItem('token')
  }
  return config
})
// 图片基础链接过滤器
Vue.filter('fixImgUrl', (oldUrl => {
  if (oldUrl.indexOf('http') === -1) {
    return axios.defaults.baseURL + oldUrl
  } else {
    return oldUrl
  }
}))
Vue.config.productionTip = false

import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
  state: {
    count: 0,
    token: 0,
    list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  },
  mutations: {
    add(state) {
      state.count += 1
    },
    token(state, data) {
      state.token = data
      console.log(state.token);
    }
  },
  actions: {
    out(context) {
      setTimeout(() => {
        const res = 3
        context.commit('token', res)
      }, 1000);
    }
  },
  getters: {
    str: store => store.list.filter(item => item > 5),
    arr(store) {
      return store.list.filter(item => {
        return item < 5
      })
    }
  },
  modules: {
    user: {
      state: {
        name: '张三'
      }
    }
  }
})
new Vue({
  router,
  store,
  render: function (h) { return h(App) }
}).$mount('#app')
